﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Reporting;
using Telerik.Reporting.XmlSerialization;

using System.Net;
using System.IO;
using System.Collections;
using System.Web.Script.Serialization;
using ezCloud.Data;

namespace ezCloud.Api.Common
{
    class ReportParams
    {
        string _report;
        Hashtable _parameterValues;

        public string report
        {
            get
            {
                return _report;
            }

            set
            {
                _report = value;
            }
        }

        public Hashtable parameterValues
        {
            get
            {
                return _parameterValues;
            }

            set
            {
                _parameterValues = value;
            }
        }
    }
    public class CustomReportResolver : Telerik.Reporting.Services.Engine.IReportResolver
    {

        public ReportSource Resolve(string reportId)
        {
            //Console.WriteLine(reportId);
            //Telerik.Reporting.Report report = new Report();
            
            
            var xmlReader = new ReportXmlSerializer();
            Report report = (Report) xmlReader.Deserialize(HttpContext.Current.Server.MapPath("/Reports/" + reportId));
            var user = Common.UserUtitily.getUserProfile();
            
            //Infomation for Header and Footer in report
            ezCloudDataContext contex = new ezCloudDataContext();
            var HotelInfomation = contex.Hotels.Where(h => h.HotelId == user.HotelId).FirstOrDefault();

            //passing hidden parameters if needed
            report.ReportParameters["HotelId"].Value = user.HotelId;
            if (report.ReportParameters["UserId"] != null)
                report.ReportParameters["UserId"].Value = user.UserId;
            if (report.ReportParameters["SiteUrl"] != null)
                report.ReportParameters["SiteUrl"].Value = HttpContext.Current.Request.Url.Scheme + "://" +  HttpContext.Current.Request.Url.Authority + "/";
            if (report.ReportParameters["Username"] != null)
            {
                report.ReportParameters["Username"].Value = user.FullName;
            }
            var context = new ezCloudDataContext();
            var hotel = context.Hotels.FirstOrDefault(h => h.HotelId == user.HotelId);
            if (report.ReportParameters["DefaultCurrency"] != null)
            {
                var currency = context.Money.FirstOrDefault(c => c.HotelId == user.HotelId && ((hotel.DefaultMoneyId != null && c.MoneyId == hotel.DefaultMoneyId) || (hotel.DefaultMoneyId == null)));
                if (currency != null)
                {
                    report.ReportParameters["DefaultCurrency"].Value = currency.MoneyName;
                } else
                {
                    report.ReportParameters["DefaultCurrency"].Value = "Vnd";
                }
                //
                if (report.ReportParameters["FormatDecimal"] != null)
                {
                    report.ReportParameters["FormatDecimal"].Value = "{0:N" + currency.Currencies.MinorUnit +"}";
                }
            }
            

            if (report.ReportParameters["HotelName"] != null)
            {
                report.ReportParameters["HotelName"].Value = HotelInfomation.HotelName;
            }

            if (report.ReportParameters["HotelAddress"] != null)
            {
                report.ReportParameters["HotelAddress"].Value = HotelInfomation.HotelAddress;
            }

            if (report.ReportParameters["HotelEmail"] != null)
            {
                report.ReportParameters["HotelEmail"].Value = HotelInfomation.HotelEmail;
            }
            if (report.ReportParameters["HotelWebsite"] != null)
            {
                report.ReportParameters["HotelWebsite"].Value = HotelInfomation.HotelWebsite==null ? "" : HotelInfomation.HotelWebsite;
            }
            if (report.ReportParameters["HotelPhone"] != null)
            {
                report.ReportParameters["HotelPhone"].Value = HotelInfomation.HotelPhone;
            }

            if (report.ReportParameters["HotelLogoUrl"] != null)
            {
                report.ReportParameters["HotelLogoUrl"].Value = HotelInfomation.HotelLogoUrl;
            }

            if (report.ReportParameters["TimeZoneOffset"] != null)
            {
                report.ReportParameters["TimeZoneOffset"].Value = HotelInfomation.TimeZoneOffset;
            }

            if (report.ReportParameters["ReportName"] != null)
            {
                report.ReportParameters["ReportName"].Value = reportId.Replace(".trdx","");
            }

          

            /*if (reportId == "RoomInvoice72mm.trdx")
            {
                try {
                    var stream = HttpContext.Current.Request.InputStream;
                    StreamReader sr = new StreamReader(stream);
                    var str = sr.ReadToEnd();
                    var serializer = new JavaScriptSerializer();
                    ReportParams rp = (ReportParams)serializer.Deserialize(str, typeof(ReportParams));
                    if (rp.parameterValues["ReservationRoomId"] != null)
                    {
                        int rrid = Int32.Parse(rp.parameterValues["ReservationRoomId"].ToString());


                        var itemCount = context.ReportItems.Where(ri => ri.ReservationRoomId == rrid).Count();
                        report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom;
                        report.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(80, Telerik.Reporting.Drawing.UnitType.Mm), new Telerik.Reporting.Drawing.Unit(10 + itemCount * 8.7,Telerik.Reporting.Drawing.UnitType.Mm));
                    }
                } catch (Exception ex) { }
            }*/
            return new Telerik.Reporting.InstanceReportSource { ReportDocument = report };
        }
        
    }
}